<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>车辆信息管理</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
  <link rel="stylesheet" href="${pageContext.request.contextPath}/css/purchase.css">
  <link rel="stylesheet" href="${pageContext.request.contextPath}/css/salesman/vehiclelist.css">
</head>
<body>
<!-- 顶部导航 -->
<header class="dashboard-header">
  <div class="container">
    <div class="d-flex justify-content-between align-items-center">
      <div>
        <h1 class="h3 mb-0"><i class="bi bi-bicycle"></i> 电动车销售管理系统</h1>
        <p class="mb-0 opacity-75">业务员工作台</p>
      </div>
      <div class="d-flex align-items-center">
        <div class="me-3">
          <span class="d-none d-md-inline">${currentUser.name}</span>
          <div class="badge bg-light text-dark">
            <i class="bi bi-person-circle me-1"></i> 业务员
          </div>
        </div>
        <div class="dropdown">
          <button class="btn btn-light rounded-circle p-1" type="button" data-bs-toggle="dropdown">
            <i class="bi bi-gear fs-5"></i>
          </button>
          <ul class="dropdown-menu dropdown-menu-end">
            <li><a class="dropdown-item" href="${pageContext.request.contextPath}/changePassword"><i class="bi bi-lock me-2"></i> 修改密码</a></li>
            <li><hr class="dropdown-divider"></li>
            <li><a class="dropdown-item text-danger" href="logout"><i class="bi bi-box-arrow-right me-2"></i> 退出登录</a></li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</header>

<div class="container py-4">
  <!-- 面包屑导航 -->
  <nav aria-label="breadcrumb" class="mb-4">
    <ol class="breadcrumb">
      <li class="breadcrumb-item"><a href="${pageContext.request.contextPath}/purchase/list">进车管理</a></li>
      <li class="breadcrumb-item active" aria-current="page">车辆信息</li>
    </ol>
  </nav>

  <div class="page-title">
    <div class="d-flex justify-content-between align-items-center">
      <div>
        <h2 class="mb-0"><i class="bi bi-bicycle me-2"></i>车辆信息管理</h2>
        <p class="text-muted mb-0">管理电动车基础信息</p>
      </div>
      <div>
        <a href="${pageContext.request.contextPath}/vehicle/new" class="btn btn-primary">
          <i class="bi bi-plus-lg me-1"></i> 添加新车辆
        </a>
      </div>
    </div>
  </div>

  <!-- 消息提示 -->
  <c:if test="${not empty successMessage}">
    <div class="alert alert-success alert-dismissible fade show" role="alert">
      <i class="bi bi-check-circle me-2"></i> ${successMessage}
      <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
    <c:remove var="successMessage" scope="session"/>
  </c:if>

  <c:if test="${not empty errorMessage}">
    <div class="alert alert-danger alert-dismissible fade show" role="alert">
      <i class="bi bi-exclamation-triangle me-2"></i> ${errorMessage}
      <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
    <c:remove var="errorMessage" scope="session"/>
  </c:if>

  <c:choose>
    <c:when test="${not empty vehicles}">
      <div class="row row-cols-1 row-cols-md-2 row-cols-lg-3 g-4">
        <c:forEach items="${vehicles}" var="vehicle">
          <div class="col">
            <div class="vehicle-card card h-100">
              <div class="actions">
                <a href="${pageContext.request.contextPath}/vehicle/edit?id=${vehicle.id}"
                   class="btn btn-warning action-btn" title="编辑">
                  <i class="bi bi-pencil"></i>
                </a>
                <a href="${pageContext.request.contextPath}/vehicle/delete?id=${vehicle.id}"
                   class="btn btn-danger action-btn delete-btn" title="删除"
                   onclick="return confirm('确定要删除该车辆吗？')">
                  <i class="bi bi-trash"></i>
                </a>
              </div>
              <c:choose>
                <c:when test="${not empty vehicle.pic}">
                  <img src="${pageContext.request.contextPath}/images/vehicles/${vehicle.pic}"
                       class="vehicle-img" alt="${vehicle.brand}">
                </c:when>
                <c:otherwise>
                  <div class="default-img">
                    <i class="bi bi-image fs-1"></i>
                  </div>
                </c:otherwise>
              </c:choose>
              <div class="card-body">
                <h5 class="card-title">${vehicle.brand} ${vehicle.model}</h5>
                <div class="d-flex justify-content-between mb-2">
                  <span class="badge bg-primary">${vehicle.color}</span>
                  <span class="text-muted">${vehicle.productionYear}年款</span>
                </div>
                <div class="d-flex justify-content-between align-items-center">
                  <span class="h5 text-primary">¥${vehicle.price}</span>
                  <a href="${pageContext.request.contextPath}/vehicle/view?id=${vehicle.id}"
                     class="btn btn-sm btn-outline-primary">
                    详情 <i class="bi bi-arrow-right"></i>
                  </a>
                </div>
              </div>
            </div>
          </div>
        </c:forEach>
      </div>
    </c:when>
    <c:otherwise>
      <div class="text-center py-5">
        <i class="bi bi-bicycle fs-1 text-muted"></i>
        <h5 class="mt-3">暂无车辆信息</h5>
        <p class="text-muted">您还没有添加任何车辆信息，点击"添加新车辆"开始创建</p>
        <a href="${pageContext.request.contextPath}/vehicle/new" class="btn btn-primary mt-2">
          <i class="bi bi-plus-lg me-1"></i> 添加新车辆
        </a>
      </div>
    </c:otherwise>
  </c:choose>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
  // 消息自动消失
  setTimeout(() => {
    const messages = document.querySelectorAll('.message');
    messages.forEach(msg => {
      msg.style.opacity = '0';
      setTimeout(() => msg.style.display = 'none', 300);
    });

    // 发送 AJAX 请求，通知服务器清空消息
    fetch('${pageContext.request.contextPath}/ClearMessageServlet', {
      method: 'GET',
      headers: {
        'X-Requested-With': 'XMLHttpRequest'
      }
    });
  }, 3000);
</script>
</body>
</html>